# This demonstration script creates a canvas widget with a text
# item that can be edited and reconfigured in various ways.
#
# @(#) ctext.tcl 1.2 95/07/18 08:31:13
set w .ctext
catch {destroy $w}
toplevel $w
wm title $w "Canvas Text Demonstration"
wm iconname $w "Text"
positionWindow $w
set c $w.c
label $w.msg -font $font -wraplength 5i -justify left -text "This window displays a string of text to demonstrate the text facilities of canvas widgets. You can click in the boxes to adjust the position of the text relative to its positioning point or change its justification. The text also supports the following simple bindings for editing:
1. You can point, click, and type.
2. You can also select with button 1.
3. You can copy the selection to the mouse position with button 2.
4. Backspace and Control+h delete the selection if there is one;
otherwise they delete the character just before the insertion cursor.
5. Delete deletes the selection if there is one; otherwise it deletes
the character just after the insertion cursor."
pack $w.msg -side top
frame $w.buttons
pack $w.buttons -side bottom -expand y -fill x -pady 2m
set textFont -*-Helvetica-Medium-R-Normal--*-240-*-*-*-*-*-*
$c create rectangle 245 195 255 205 -outline black -fill red
# First, create the text item and give it bindings so it can be edited.
$c addtag text withtag [$c create text 250 200 -text "This is just a string of text to demonstrate the text facilities of canvas widgets. Bindings have been been defined to support editing (see above)." -width 440 -anchor n -font -*-Helvetica-Medium-R-Normal--*-240-*-*-*-*-*-* -justify left]
$c bind text <1> "textB1Press $c %x %y"
$c bind text <B1-Motion> "textB1Move $c %x %y"
$c bind text <Shift-1> "$c select adjust current @%x,%y"
$c bind text <Shift-B1-Motion> "textB1Move $c %x %y"
$c bind text <KeyPress> "textInsert $c %A"
$c bind text <Return> "textInsert $c \\n"
$c bind text <Control-h> "textBs $c"
$c bind text <BackSpace> "textBs $c"
$c bind text <Delete> "textDel $c"
$c bind text <2> "textPaste $c @%x,%y"
# Next, create some items that allow the text's anchor position
# to be edited.
proc mkTextConfig {w x y option value color} {
set item [$w create rect [expr $x] [expr $y] [expr $x+30] [expr $y+30] \
-outline black -fill $color -width 1]
$w bind $item <1> "$w itemconf text $option $value"
$w addtag config withtag $item
}
set x 50
set y 50
set color LightSkyBlue1
mkTextConfig $c $x $y -anchor se $color
mkTextConfig $c [expr $x+30] [expr $y] -anchor s $color